<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>文章类型</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/common/layui/css/layui.css" media="all">
    <script type="text/javascript" src="/static/common/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="/static/common/layui/layui.js"></script>
    <style type="text/css">
        .calss-a {
            text-decoration: underline !important;
        }

    </style>
</head>
<body>
<div class="layui-form">
    <div class="layui-input-inline" style="width: 100px;">
        <label class="layui-form-label">
            <button class="layui-btn" onclick="add()">添加</button>
        </label>
    </div>
</div>
<table class="layui-hide" id="test" lay-filter="barDemo"></table>
<script type="text/html" id="switchTpl">
    <input type="checkbox" name="sex" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="showCode" {{ d.showCode== 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<div style="display: none" id="module">
    <form class="layui-form layui-form-pane"   style="margin: 30px 30px;" lay-filter="example">
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-inline">
                <input type="text" name="type" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" maxlength="5">
            </div>
        </div>
        <input type="hidden" name="id" value="">
        <div class="layui-form-item" style="display: table; margin: 0px auto;">
            <button class="layui-btn" lay-submit="" lay-filter="button-d">提交</button>
        </div>
    </form>

</div>
</body>
<script type="text/javascript">
    var html = $('#module').html();
    $('#module').html('');

    function add() {
        layer.open({
            title: '添加分类',
            type: 1,
            area: ['420px', '240px'], //宽高
            content: html
        });
    }
</script>
<script type="text/javascript">
    layui.use(['table', 'form'], function () {
        var table = layui.table,
                form = layui.form;
        table.render({
            elem: '#test',
            url: '/article/type/list.do',
            cellMinWidth: 40,
            limit: 20,
            cols: [[
                {field: 'id', title: 'ID', width: "10%"},
                {field: 'create_time', title: '创建时间', width: "10%", sort: true},
                {field: 'modify_time', title: '修改时间', width: "10%", sort: true},
                {field: 'type', title: '类型', width: "48%", templet: '#countyTpl'},
                {field: 'showCode', title: '是否上线', width: "10%", sort: true, templet: '#switchTpl'},
                {fixed: 'right', title: '操作', width: "10%", templet: '#barDemo'},
            ]],
            page: true
        });
        //添加
        form.on('submit(button-d)', function (data) {
            $.post('/article/type/addByUpdata.do', data.field, function (res) {
                if (res.data == true) {
                    layer.msg('操作成功!', {icon: 6});
                    setTimeout("location.reload();", 1000)
                } else {
                    layer.alert('操作失败!', {icon: 5});
                }
            })
            return false;
        });
        //上下线更新
        form.on('switch(showCode)', function (data) {
            $.post('/article/type/status.do', {id: data.value}, function (res) {
                if (res.data == true) {
                    layer.msg('操作成功!', {icon: 6});
                } else {
                    layer.msg('添加失败!', {icon: 5});
                }
            })
            return false;
        });
        table.on('tool(barDemo)', function (obj){
            var  form = layui.form;
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'del') { //删除
                layer.confirm('确定要删除?', function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                    $.post("/article/type/delete.do", {id: data.id}, function (result) {
                        if (result.data == true) {
                            layer.msg('删除成功!', {icon: 1});
                        } else {
                            layer.msg('删除失败!', {icon: 2});
                        }
                    }, "json");
                });
            }else if(layEvent === 'edit'){
                    layer.open({
                        title: '编辑分类',
                        type: 1,
                        area: ['420px', '240px'], //宽高
                        content: html
                    });
                //表单初始赋值
                form.val('example',data);
            }
            return false;
        });
    });
</script>
<script src="/static/common/js/ajaxhook.js" type="text/javascript"></script>
<script src="/static/common/js/ajaxHookData.js" type="text/javascript"></script>
</html>